package p3.cse;

import java.util.ArrayList;

import p3.cse.R.id;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * This activity presents the user interface after the user has unlocked the
 * password database. This will allow other activities to be started to modify
 * the database and perform backups.
 * 
 * @see /res/layout/unlocked.xml
 */
public final class Unlocked extends Activity {
	/**
	 * Event listener for "Add Account" button. Will trigger the add.java
	 * activity.
	 * 
	 * @see /res/layout/add.xml
	 * @see Add.java
	 */
	private OnClickListener addPassword = new OnClickListener() {
		public void onClick(final View v) {
			Backup.downloading = false;
			startActivity(new Intent(Unlocked.this, Add.class));
		}
	};

	/**
	 * This is the event listener for each account button. Will trigger the
	 * update.java activity and pass the unique account name with it for
	 * editing.
	 * 
	 * @see /res/layout/update.xml
	 * @see Update.java
	 */
	private OnClickListener updateInfo = new OnClickListener() {
		public void onClick(final View v) {
			Button b = (Button) v;

			// pass the unique account name
			Intent intent = new Intent(Unlocked.this, Update.class);
			intent.putExtra("entry_account", b.getText().toString());

			startActivity(intent);
		}
	};

	/**
	 * The event listener for "Change Password" button. Will trigger the
	 * Changepassword.java activity.
	 * 
	 * @see /res/layout/changepassword.xml
	 * @see Changepassword.java
	 */
	private OnClickListener changePassword = new OnClickListener() {
		public void onClick(final View v) {
			startActivity(new Intent(Unlocked.this, Changepassword.class));
		}
	};

	/**
	 * The event listener for the "Backup" button. Will open up a https
	 * connection to dropbox and allow sending of encrypted database file of
	 * passwords to dropbox account.
	 * 
	 * @see Backup.java
	 */
	private OnClickListener uploadBackup = new OnClickListener() {
		public void onClick(final View v) {
			Log.d("upload", "clicking upload");
			Backup.downloading = false;
			startActivity(new Intent(Unlocked.this, Backup.class));
		}
	};

	/**
	 * Sets up this activity by binding unlocked.xml button listeners and
	 * writing all accounts to show_passwords_layout layout.
	 * 
	 * @see /res/layout/unlocked.xml
	 * @see onResume()
	 * @see onCreate()
	 */
	public void initDocument() {
		setContentView(R.layout.unlocked);

		Button ok = (Button) findViewById(R.id.ChangePwButton);
		ok.setOnClickListener(changePassword);

		Button add = (Button) findViewById(R.id.Add_RemoveButton);
		add.setOnClickListener(addPassword);

		Button upload = (Button) findViewById(R.id.backupbutton);
		upload.setOnClickListener(uploadBackup);

		Filer filer = Filer.getInstance();
		int size = filer.entryCount();
		ArrayList<Button> entries = new ArrayList<Button>(size);
		int i = 0;
		LinearLayout m = (LinearLayout) findViewById(id.show_passwords_layout);
		m.removeAllViews();

		for (Entry a : filer) {
			LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			Button newButton = new Button(getBaseContext());
			Log.d("Account", a.account);
			newButton.setText(a.account);
			m.addView(newButton, p);
			entries.add(i, newButton);
			i++;

			newButton.setOnClickListener(updateInfo);
		}
	}

	/**
	 * Called when this activity is returned to by either changePassword,
	 * update, add, or backup activities.
	 * 
	 * @see Add.java
	 * @see Update.java
	 * @see Backup.java
	 * @see Changepassword.java
	 */
	@Override
	public void onResume() {
		super.onResume();
		initDocument();
	}

	/**
	 * The constructor called by Android when this activity is initialized. Main
	 * screen for application.
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initDocument();
	}
}
